package com.derbysoft.lowcostadapter.web.action.admin.cachemanager.search;

import com.derbysoft.lowcostadapter.commons.model.RoomType;
import com.derbysoft.lowcostadapter.commons.redis.service.RoomTypeCacheService;
import com.derbysoft.lowcostadapter.web.action.BaseAdapterAction;
import com.derbysoft.lowcostadapter.web.action.model.RoomTypeCondition;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by: jason
 * Date: 2012-09-26
 */
public class RoomTypeSearchAction extends BaseAdapterAction {

    private RoomTypeCondition roomTypeCondition;

    @Autowired
    private RoomTypeCacheService roomTypeCacheService;

    @Action("searchRoomType")
    public void searchRoomType() {
        try {
            roomTypeCondition.validate();
            RoomType value = roomTypeCacheService.load(roomTypeCondition.makeKey());
            process(value != null ? value.value() : NOT_FOUND);
        } catch (Exception e) {
            populateFailedJsonResult(e);
        }
    }

    private void process(String value) {
        try {
            populateSucceededJsonResult(value);
        } catch (Exception e) {
            populateFailedJsonResult(e);
        }
    }

    public RoomTypeCondition getRoomTypeCondition() {
        return roomTypeCondition;
    }

    public void setRoomTypeCondition(RoomTypeCondition roomTypeCondition) {
        this.roomTypeCondition = roomTypeCondition;
    }
}
